package com.we.risk.plist.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.we.utils.CommonTimeTools.getHourOfTime;

public class PlistTools {
    public static Logger LOG = LoggerFactory.getLogger(PlistTools.class);

    public static final String TAGTOT = "tot";
    public static final String TAGBANK = "bank";
    public static final String TAGLOAN = "loan";
    public static final String TAGBET = "bet";
    public static final String TAGJJSH = "tagjjsh";
    public static final String TAGJRLC = "tagjrlc";
    public static final String TAGLTSJ = "tagltsj";
    public static final String TAGLYJT = "taglyjt";
    public static final String TAGNONE = "tagnone";
    public static final String TAGSCTY = "tagscty";
    public static final String TAGSJYX = "tagsjyx";
    public static final String TAGSSGW = "tagssgw";
    public static final String TAGSYGJ = "tagsygj";
    public static final String TAGSYSX = "tagsysx";
    public static final String TAGTSYD = "tagtsyd";
    public static final String TAGXLBG = "tagxlbg";
    public static final String TAGXWZX = "tagxwzx";
    public static final String TAGXXJY = "tagxxjy";
    public static final String TAGXXYL = "tagxxyl";
    public static final String TAGYLJK = "tagyljk";
    public static final String TAGYYST = "tagyyst";
    public static final String TAGOTHER = "tagother";

    public static String H1_6 = "h1_6";
    public static String H7_12 = "h7_12";
    public static String H13_18 = "h13_18";
    public static String H19_24 = "h19_24";

    public static final String bankPattern =
            "银行|信用卡|农商行|银联|bank|掌上生活|渣打财富管理|" + "HAI生活|云南农信|发现精彩|卡动空间|浦大喜奔|工银联e融|无锡农商";

    public static final String loanAppPattern =
            "借钱|钱包|钱袋|口袋|贷款|贷|借|借贷|借钱|借条|钱|钱庄|" + "下款|普惠|快贷|易贷|金融|呗|分期|融360|闪银|现金|宜人";
    public static final String loanApkPattern =
            "koudai|jieqian|daikuan|qianbao|loan|qianzhuang|"
                    + "kuaidai|yidai|wallet|fenqi|money|cash|rong360";

    public static final String betAppPattern =
            "今彩|大馬彩|大马彩|威力彩|六合|乐透|樂透|澳門|星彩|雙贏彩|菠菜|博彩|博金|彩票|投注|Bet|bet|水位|盘口|赌场|"
                    + "娱乐城|银河娱乐|合彩|葡京|澳门|足彩|红单|体彩|外围|开奖|双色球|toto|TOTO|Toto|lottery|Lottery|"
                    + "lotto|Lotto|casino|Casino|老虎机|轮盘|百家乐|Slot|slot|雙色球|老虎機|輪盤|賭場|盤口|百家樂";
    public static final String betApkPattern = "lottery|caipiao|lotto|casino";

    public static String getHourPart(long time) {
        int part = getHourOfTime(time);
        if (part >= 1 && part <= 6) {
            return H1_6;
        } else if (part >= 7 && part <= 12) {
            return H7_12;
        } else if (part >= 13 && part <= 18) {
            return H13_18;
        } else if ((part >= 19 && part <= 23) || part == 0) {
            return H19_24;
        } else {
            LOG.error("getHourPart wrong param: " + time);
            return null;
        }
    }

    public static byte getLoanTag(String appName, String apkName) {
        if (appName != null && !appName.equals("好分期")) {
            if (rlikeInJava(appName, loanAppPattern) || rlikeInJava(apkName, loanApkPattern)) {
                return 1;
            }
        }
        return 0;
    }

    public static byte getBankTag(String appName) {
        if (appName != null) {
            if (rlikeInJava(appName, bankPattern)) {
                return 1;
            }
        }
        return 0;
    }

    public static byte getBetTag(String appName, String apkName) {
        if (appName != null) {
            if (rlikeInJava(appName, betAppPattern) || rlikeInJava(apkName, betApkPattern)) {
                return 1;
            }
        }
        return 0;
    }

    private static boolean rlikeInJava(String input, String pattern) {
        Pattern p = Pattern.compile(pattern);
        Matcher matcher = p.matcher(input);
        return matcher.find();
    }

    private static String mapCateTag(String cateName) {
        switch (cateName) {
            case "休闲娱乐":
                return TAGXXYL;
            case "医疗健康":
                return TAGYLJK;
            case "图书阅读":
                return TAGTSYD;
            case "学习教育":
                return TAGXXJY;
            case "实用工具":
                return TAGSYGJ;
            case "居家生活":
                return TAGJJSH;
            case "影音视听":
                return TAGYYST;
            case "手机游戏":
                return TAGSJYX;
            case "摄影摄像":
                return TAGSYSX;
            case "效率办公":
                return TAGXLBG;
            case "新闻资讯":
                return TAGXWZX;
            case "旅行交通":
                return TAGLYJT;
            case "时尚购物":
                return TAGSSGW;
            case "聊天社交":
                return TAGLTSJ;
            case "赛车体育":
                return TAGSCTY;
            case "金融理财":
                return TAGJRLC;
            default:
                return TAGOTHER;
        }
    }

    public static ArrayList<String> getAppTag(
            String appName,
            String apkName,
            HashMap<String, ArrayList<String>> appMap,
            HashMap<String, ArrayList<String>> apkMap) {
        ArrayList<String> apkTagList = apkMap.get(apkName.toLowerCase());
        ArrayList<String> appTagList = appMap.get(appName.toLowerCase());
        /** TagMap 存在一对多情况 */
        ArrayList<String> resList = new ArrayList<>();
        HashSet<String> tagSets = new HashSet<>();
        if (apkTagList != null) {
            for (String cate : apkTagList) {
                if (!tagSets.contains(cate)) {
                    tagSets.add(cate);
                    resList.add(mapCateTag(cate));
                }
            }
        } else if (appTagList != null) {
            for (String cate : appTagList) {
                if (!tagSets.contains(cate)) {
                    tagSets.add(cate);
                    resList.add(mapCateTag(cate));
                }
            }
        }
        if (resList.size() == 0) {
            resList.add(TAGNONE);
        }

        return resList;
    }

    public static void main(String[] args) {
        String appName = "中国建设银行"; // 银联可信服务安全组件
        String apkName = "中国建设银行";

        System.out.println("loanTag:" + getLoanTag(appName, apkName));
        System.out.println("bankTag:" + getBankTag(appName));
        System.out.println("betTag:" + getBetTag(appName, apkName));
    }
}
